<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>角色管理</span>
      </div>
      <div class="text item">
        <!-- 搜索区域 -->
        <el-form :inline="true" :model="queryParams" class="demo-form-inline">
          <el-form-item label="角色名称">
            <el-input v-model="queryParams.roleName" placeholder="请输入角色名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button v-if="checkPermission(['system:role:add'])" type="success" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-form>

        <!-- 表格区域 -->
        <el-table :data="roleList" style="width: 100%" v-loading="loading">
          <el-table-column prop="roleName" label="角色名称"></el-table-column>
          <el-table-column prop="roleKey" label="角色标识"></el-table-column>
          <el-table-column prop="description" label="描述"></el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
                {{ scope.row.status === 1 ? '正常' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="scope">
              <el-button v-if="checkPermission(['system:role:edit'])" 
                type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button v-if="checkPermission(['system:role:delete'])" 
                type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
              <el-button type="warning" size="mini" @click="handlePermission(scope.row)">权限</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页区域 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="form.roleName" placeholder="请输入角色名称"></el-input>
        </el-form-item>
        <el-form-item label="角色标识" prop="roleKey">
          <el-input v-model="form.roleKey" placeholder="请输入角色标识"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="form.description" placeholder="请输入描述"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分配权限对话框 -->
    <el-dialog title="分配权限" :visible.sync="permissionDialogVisible" width="600px" append-to-body>
      <el-tree
        ref="menuTree"
        :data="menuOptions"
        :props="defaultProps"
        show-checkbox
        node-key="id">
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="permissionDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitPermission">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Role',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: ''
      },
      // 弹出层标题
      dialogTitle: '',
      // 弹出层是否显示
      dialogVisible: false,
      // 权限弹出层是否显示
      permissionDialogVisible: false,
      // 表单参数
      form: {
        id: undefined,
        roleName: undefined,
        roleKey: undefined,
        description: undefined,
        status: 1
      },
      // 表单校验
      rules: {
        roleName: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        roleKey: [
          { required: true, message: '角色标识不能为空', trigger: 'blur' }
        ]
      },
      // 菜单树选项
      menuOptions: [],
      // 菜单树配置
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission(permissions) {
      // TODO: 实现权限检查
      return true
    },
    async getList() {
      this.loading = true
      try {
        // TODO: 调用角色列表API
        // 模拟数据
        this.roleList = [
          {
            id: 1,
            roleName: '管理员',
            roleKey: 'admin',
            description: '系统管理员',
            status: 1
          }
        ]
        this.total = this.roleList.length
        this.loading = false
      } catch (error) {
        this.loading = false
        console.error('获取角色列表失败:', error)
      }
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleAdd() {
      this.dialogTitle = '添加角色'
      this.form = {
        id: undefined,
        roleName: undefined,
        roleKey: undefined,
        description: undefined,
        status: 1
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑角色'
      this.form = { ...row }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该角色吗？', '提示', {
        type: 'warning'
      }).then(() => {
        // TODO: 调用删除API
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {})
    },
    handlePermission(row) {
      // TODO: 获取菜单树数据
      this.menuOptions = [
        {
          id: 1,
          name: '系统管理',
          children: [
            { id: 2, name: '用户管理' },
            { id: 3, name: '角色管理' },
            { id: 4, name: '菜单管理' }
          ]
        }
      ]
      this.permissionDialogVisible = true
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          // TODO: 调用保存API
          this.$message.success(this.form.id ? '修改成功' : '新增成功')
          this.dialogVisible = false
          this.getList()
        }
      })
    },
    submitPermission() {
      const checkedKeys = this.$refs.menuTree.getCheckedKeys()
      // TODO: 调用保存权限API
      this.$message.success('权限分配成功')
      this.permissionDialogVisible = false
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}
</style> 